﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication2.Model
{
    class Transaksi
    {        
        public int ID_Transaksi;
        DateTime TanggalMasuk;
        DateTime TanggalKeluar;
        int ID_Konsumen;
        public int TotalHarga;
        int Nominal;
        int Persentase;
        int Correction;
        
        public Transaksi()
        {
            
        }

        public Transaksi(int _ID_Transaksi)
        {
            Database db = new Database();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            string sql = "SELECT * FROM transaksi WHERE \"ID_Transaksi\" = " + _ID_Transaksi.ToString() + "";

            db.Open();
            ds = db.ExecuteQuery(sql);
            dt = ds.Tables[0];
            DataRow dr = dt.Rows[0];

            ID_Transaksi = (int)dr[0];
            TanggalMasuk = (DateTime)dr[1];
            TotalHarga = (int)dr[4];
            
            db.Close();
        }

        public void AddTransaksi(string _TanggalMasuk, int _ID_Konsumen)
        {
            Database db = new Database();
            long l;
            string sql = "INSERT INTO transaksi(\"TanggalMasuk\", \"ID_Konsumen\") VALUES ('" + _TanggalMasuk + "'," + _ID_Konsumen.ToString() + ");select currval('\"Transaksi_ID_Transaksi_seq\"')";

            db.Open();
            l = db.ExecuteNonQuery(sql);
            db.Close();

            ID_Transaksi = (int)l;

            System.Console.WriteLine(l.ToString());
        }

        public void AddTransaksi(string _TanggalMasuk, int _ID_Konsumen, int _totalHarga)
        {
            Database db = new Database();
            long l;
            string sql = "INSERT INTO transaksi(TanggalMasuk, ID_Konsumen, TotalHarga) VALUES ('" + _TanggalMasuk + "'," + _ID_Konsumen.ToString() + "," + _totalHarga.ToString() + ");select currval('Transaksi_ID_Transaksi_seq')";

            db.Open();
            l = db.ExecuteNonQuery(sql);
            db.Close();

            System.Console.WriteLine(l.ToString());
        }

        public void UpdateTransaksi(int _idTransaksi, int _harga)
        {
            Database db = new Database();
            string sql = "UPDATE transaksi SET \"TotalHarga\" = " + _harga.ToString() + " WHERE \"ID_Transaksi\" = " + _idTransaksi.ToString();

            db.Open();
            db.ExecuteDelete(sql);
            db.Close();
        }

        public void DeleteTransaksi(int _idTransaksi)
        {
            Database db = new Database();
            string sql = "DELETE FROM transaksi WHERE \"ID_Transaksi\" = " + _idTransaksi.ToString();

            db.Open();
            db.ExecuteDelete(sql);
            db.Close();
        }
    }
}
